<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'海报列表'),
	array('op'=>'edit','title'=>'添加海报'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['plugin']="jy_inclock";
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','isdefault','follows','times','type');
	$list=pdo_getslice('jy_poster_poster',$params,array($pindex,$psize),$total,$fields,'','id desc');
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'times','title'=>'扫描数','type'=>'string'),
			array('name'=>'follows','title'=>'关注数','type'=>'string'),
		
		),
		'action'=>array(
			array('op'=>'sj','title'=>'设计海报','type'=>'href'),
			array('op'=>'qc','title'=>'清除海报缓存','type'=>'href'),
			
		)
		),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$return['type']=array('1'=>'商城','2'=>'小店','3'=>'商品','4'=>'分销');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_poster_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'基本信息',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回海报列表'),
		),
		'edit_tab'=>true,
		'edit'=>array(
					array('name'=>'title','title'=>'海报名称','type'=>'string'),

					array('name'=>'waittext','title'=>'等待生成文字','type'=>'string','help'=>'例如：您的专属海报正在拼命生成中，请等待片刻...'),	
					
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'waittext'=>trim( $postdata['waittext']),
		'plugin'=>"jy_inclock",
	);

	$id=intval($postdata['id']);
	if($id){
		$da=array('backtype'=>8,'key'=>$id,'status'=>1,'title'=>'海报'.$id,'event'=>'create','keyword'=>trim( $postdata['keyword']));
		cfc('core')->save_back($da,'jy_poster');
		pdo_update('jy_poster_poster',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_poster_poster',$data);
		$id=pdo_insertid();
		$da=array('backtype'=>8,'key'=>$id,'status'=>1,'title'=>'海报'.$id,'event'=>'create','keyword'=>trim( $postdata['keyword']));
		cfc('core')->save_back($da,'jy_poster');
	}
	
	exi('操作成功！','','list');
}
if($_RQ['op']=='sj'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_poster_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	
	$item['data']=json_decode($item['data'],true);
	$item['image']=$item['data']['image'];
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='qc'){
	$handle = opendir( ROOT_AT .'/images/jy_poster/' . $_SESSION['uniacid'] );
		while ( false != ( $item = readdir( $handle ) ) ) {
			if ( $item != '.' && $item !='..' ) { 
			unlink(ROOT_AT .'/images/jy_poster/' . $_SESSION['uniacid'].'/'.$item);
			}
		}			
	closedir( $handle ); 
	exi('清除成功','','list');
	}  

if($_RQ['op']=='design'){
	$postdata=get_postdata();
	$id=$postdata['id'];
	if(!$id){
		exi('请选择海报','error');
	}
	$data['data']=$postdata['data'];
	$data['image']=$postdata['image'];
	pdo_update('jy_poster_poster',array('data'=>json_encode($data)),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('保存成功','error','list');
}

if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_poster_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_poster_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
